package com.dullwolf.utils;

import com.dullwolf.pojo.Comments;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class JsoupUtil {

    //爬虫工具类
    public List<Comments> spaderJD(String keyWords)throws Exception{
        String url="https://search.jd.com/Search?keyword="+keyWords+"&pvid=9045b0472b9f4c17bdf98ec6037a8afc";
        Document document = Jsoup.parse(new URL(url),30000);
        Element list = document.getElementById("J_goodsList");
        Elements li = list.getElementsByTag("li");
        ArrayList<Comments> commentsArrayList = new ArrayList<>();
        for (Element element:li){
            String src = element.getElementsByTag("img").attr("src");
            String price = element.getElementsByClass("p-price").eq(0).text();
            String name = element.getElementsByClass("p-name").eq(0).text();
            Comments comments = new Comments();
            comments.setImg(src);
            comments.setPrice(price);
            comments.setName(name);
            commentsArrayList.add(comments);
        }
        return commentsArrayList;
    }

    public static void main(String[] args) throws Exception{
        JsoupUtil jsoupUtil = new JsoupUtil();
        jsoupUtil.spaderJD("java").forEach(System.out::println);
    }
}